package com.mobogenie.blur;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;


public class DownloadManagerRecommendedView extends LinearLayout {

    private View mRootView;
    private Context mContext;

    private GridView mGridView;
    private DownloadManagerRecommendedAdapter mAdapter;

    public DownloadManagerRecommendedView(Context context) {
        super(context);
        initViews(context);
    }

    public DownloadManagerRecommendedView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        mContext = context;
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        mRootView = layoutInflater.inflate(R.layout.download_recommended_layout, this, false);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(mRootView, layoutParams);

        mGridView = (GridView) findViewById(R.id.download_manager_recommended_list);
        mGridView.setCacheColorHint(Color.TRANSPARENT);
        mAdapter = new DownloadManagerRecommendedAdapter(context);
        mGridView.setAdapter(mAdapter);

        int size = mAdapter.getAllSamples().size();
        if (size != 0) {
            int line = size / 2;
            if (size % 2 != 0) {
                line++;
                double height = 76.5 * 3 * line;
                ViewGroup.LayoutParams params = mGridView.getLayoutParams();
                params.height = (int) height;
                mGridView.setLayoutParams(params);
            }
        }

        findViewById(R.id.app_install_2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "xiangjt", Toast.LENGTH_LONG).show();
            }
        });
    }
}
